import {useEffect, useState} from "react";
import useGetUserInfo from "./useGetUserInfo";
import {useRequest} from "ahooks";
import {getUserInfoService} from "../services/user";
import {useDispatch} from "react-redux";
import {loginReducer} from "../store/userReducer";

export default function useLoadUserData() {
    const [waitingUserData, setWaitingUserData] = useState(true)
    const dispatch = useDispatch()
    const {run} = useRequest(getUserInfoService, {
        manual: true,
        onSuccess(result) {
            const {username, nickname} = result
            dispatch(loginReducer({username, nickname}))
        },
        onFinally() {
            setWaitingUserData(false)
        }
    })
    const {username} = useGetUserInfo()
    useEffect(() => {
        if (username) return setWaitingUserData(false)
        run()
    }, [username]);
    return {waitingUserData}
}